﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataExtractionLanguage;
using System.IO;
using System.Data;

namespace WrapperPresentation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnExecute_Click(object sender, RoutedEventArgs e)
        {
            TabItem tabItem = null;

            string sourceCode = txtCodeEditor.Text;
            Output output = new Output();
            Report report = new Report();
            ILexical lexer = new Lexical(sourceCode);
            SyntaxAnalyzer parser = new SyntaxAnalyzer(lexer, report);
            ExecutionUnit executor = new ExecutionUnit(report);
            DataTable dataTable = null;

            try
            {
                parser.ReadInstructions();
                dataTable = executor.Execute(parser.ParsingTree);
            }
            catch (Exception ex)
            {
                if (ex.Message != "Error")
                    MessageBox.Show(ex.Message);
            }

            tabResult.Items.Clear();

            if (report.ErrorsReport.Count > 0)
            {
                tabItem = new TabItem();
                tabItem.Header = "Errors";
                tabItem.Content = new DataGrid { ItemsSource = CollectionViewSource.GetDefaultView(report.ErrorsReport) };
                tabResult.Items.Add(tabItem);
                tabResult.SelectedItem = tabItem;
            }
            else
            {
                tabItem = new TabItem();
                tabItem.Header = dataTable.TableName; ;

                DataGrid grd = new DataGrid();
                grd.ItemsSource = CollectionViewSource.GetDefaultView(dataTable);
                tabItem.Content = grd;
                tabResult.Items.Add(tabItem);
                tabResult.SelectedIndex = 0;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            using (FileStream stream = new FileStream(Environment.CurrentDirectory + @"\codeFile3.txt", FileMode.Open))
            using (StreamReader sr = new StreamReader(stream))
                txtCodeEditor.Text = sr.ReadToEnd();
            //txtCodeEditor.Text = "select Dm1.html.body.div[2].div.div[2].div.div.div.div.div[2].div.div[3].div.table.tbody.tr[4].td.div\r\n{\r\n.div[1].Text As CategoryName, .div[2].a.Text As SubjectName, .div[2].a.GetAttribute(\"href\") As SubjectNameUrl, .div[3].Text.SubString(1, Description.Text.Length - 2) As Description, .div[4].Text as Source\r\n}\r\nFROM \"D:\\Tanmia\\SaryaSolution\\AlefWork\\DEL\\CodeProjectNews1\\*.htm\" As Dm1\r\nWHERE SubjectNameUrl.GetAttribute(\"href\").Like(\"448\")";
        }

        private void txtCodeEditor_SelectionChanged(object sender, RoutedEventArgs e)
        {
            int lineIndex = txtCodeEditor.GetLineIndexFromCharacterIndex(txtCodeEditor.SelectionStart);
            sbLn.Content = string.Format("Ln {0}", lineIndex);
            sbCol.Content = string.Format("Col {0}", txtCodeEditor.GetCharacterIndexFromLineIndex(lineIndex));
        }
    }
}